/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.com.sml.SEFM.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.mvc.SimpleFormController;
import vn.com.sml.SEFM.BUS.MerchantBUS;
import vn.com.sml.SEFM.BUS.UserBUS;
import vn.com.sml.SEFM.BUS.UserRolesBUS;
import vn.com.sml.SEFM.BUS.UserStatusBUS;
import vn.com.sml.SEFM.DAO.UserDAO;
import vn.com.sml.SEFM.domain.Merchant;
import vn.com.sml.SEFM.domain.User;
import vn.com.sml.SEFM.domain.UserRoles;
import vn.com.sml.SEFM.domain.Userstatus;

/** 
 *
 * @author Tunp
 */
public class UpdateUserController extends SimpleFormController implements Controller {
 private UserBUS userBUS;
    private MerchantBUS dvcntBUS;
    private UserRolesBUS userrolesBUS;
    private UserStatusBUS userstatusBUS;

    public void setDvcntBUS(MerchantBUS dvcntBUS) {
        this.dvcntBUS = dvcntBUS;
    }

    public void setUserrolesBUS(UserRolesBUS userrolesBUS) {
        this.userrolesBUS = userrolesBUS;
    }

    public void setUserstatusBUS(UserStatusBUS userstatusBUS) {
        this.userstatusBUS = userstatusBUS;
    }

    public void setUserBUS(UserBUS userBUS) {
        this.userBUS = userBUS;
    }
    
    public UpdateUserController() {
        //Initialize controller properties here or 
        //in the Web Application Context

        setCommandClass(User.class);
        setCommandName("user");
        setSuccessView("DetailUser");
        setFormView("UpdateUser");
    }
    
    //Use onSubmit instead of doSubmitAction 
    //when you need access to the Request, Response, or BindException objects
    
    @Override
    protected ModelAndView showForm(HttpServletRequest request, 
        HttpServletResponse response, BindException errors) throws Exception {
        
        ModelAndView mv = super.showForm(request, response, errors);
        
        String id = (request.getParameter("userId"));
        
        int uId = Integer.parseInt(id);
        User us = userBUS.getUserById(uId);
        mv.addObject("user", us);
        
        List<Merchant> lstDvcnt = dvcntBUS.listDvcnt();
        mv.addObject("lstDvcnt",lstDvcnt);
        
        List<UserRoles> lstUserrole = userrolesBUS.listUserRoles();
        mv.addObject("lstUserrole",lstUserrole);
        
        List<Userstatus> lstUserstt = userstatusBUS.listUserstatus();
        mv.addObject("lstUserstt",lstUserstt);
        
        return mv;
    }
    
    @Override
    protected ModelAndView onSubmit(
    HttpServletRequest request, 
    HttpServletResponse response, 
    Object command, 
    BindException errors) throws Exception {
        
        ModelAndView mv = new ModelAndView(getSuccessView());
        //Do something...
    
        User us = (User) command;

        int DvcntId = Integer.parseInt(request.getParameter("DvcntId"));
        int UserroleId = Integer.parseInt(request.getParameter("UserroleId"));
        int statusId = Integer.parseInt(request.getParameter("userstatusId"));
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();

        us.setMerchant(dvcntBUS.getDvcntById(DvcntId));
        us.setUserRoles(userrolesBUS.getUserRolesById(UserroleId));
        us.setUserstatus(userstatusBUS.getUserstatusById(statusId));
        //us.setCreatedTime(dateFormat.format(date).toString());
        us.setChangestatusTime(dateFormat.format(date).toString());

        userBUS.UpdateUser(us);
        mv.addObject("user",us);

        return mv;
    }
     
}
